home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 38 / Amiga Format CD38 (1999-03-15)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-04].iso / -screenplay- / hd_installers / -whdload- / whdload_dev / src / programs / cmp.c < prev    next >
C/C++ Source or Header  |  1998-06-22  |  3KB  |  123 lines

  1. /*
  2.     a little, simple, quick cmp program
  3.     advantages against the thousend other cmp's out there:
  4.         - command line only
  5.         - wide output hex And ascii (this was the reason for writing)
  6.         - does not need to load whole file at once
  7.         - std c, should be eatable by every compiler
  8.  
  9.     the included binary is compiled using gcc, requires ixemul.library + mc68020
  10.     released under GNU Public License
  11.     
  12.     wepl, sometime ago ...
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. #define buflen 32768
  18. #define error1 { perror(argv[1]); exit(20); }
  19. #define error2 { perror(argv[2]); exit(20); }
  20. #define maxpl 16    /* displayed diffs per line */
  21.  
  22. int opt_quick=1;
  23.  
  24. /**********************/
  25.  
  26. void cmpout(unsigned char *c1, unsigned char *c2, int p1, int p2, int len) {
  27.   int j;
  28.   unsigned char *t;
  29.  
  30.   if (opt_quick == 0) return;
  31.   
  32.   printf("%06x ",p1);                /* file1 offset */
  33.   for (t=c1,j=0;j<len;j++) printf("%02x",*t++);
  34.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  35.   for (j=0;j<len;j++,c1++) putchar(*c1 < ' ' || *c1 > 127 ? '.' : *c1);
  36.   for (j=0;j<=maxpl-len;j++) putchar(' ');
  37.   if  (p1 != p2) printf("%06x ",p2);        /* file2 offset */
  38.   for (t=c2,j=0;j<len;j++) printf("%02x",*t++);
  39.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  40.   for (j=0;j<len;j++,c2++) putchar(*c2 < ' ' || *c2 > 127 ? '.' : *c2);
  41.   putchar('\n');
  42. }
  43.  
  44. /**********************/
  45.  
  46. int cmp(char *m1, char *m2, int o1, int o2, int len) {
  47.   int i, diffs=0;
  48.   char t1[maxpl+1]="", t2[maxpl+1]="";
  49.   int dc=0,    /* count of bytes in buff */
  50.       ds;    /* filepos of first stored byte in buff */
  51.   
  52.   for (i=0;i<len;i++) {
  53.     if (*m1++ != *m2++) {
  54.       /* output if the new cannot appended */
  55.       if ( dc > 0 && ds+dc != i) {
  56.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  57.         dc = 0;
  58.       }
  59.       /* append */
  60.       if (dc == 0) ds = i;
  61.       t1[dc]   = *(m1-1);
  62.       t2[dc++] = *(m2-1);
  63.       /* output if buff full */
  64.       if (dc == maxpl) {
  65.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  66.         dc=0;
  67.       }
  68.       diffs++;
  69.     }
  70.   }
  71.   /* output if bytes left in buff */
  72.   if (dc > 0 )
  73.     cmpout(t1,t2,o1+ds,o2+ds,dc);
  74.   return diffs;
  75. }
  76.  
  77. /**********************/
  78.  
  79. long getfilesize(FILE *fp) {
  80.   fseek(fp,0,SEEK_END);
  81.   return ftell(fp);
  82. }
  83.  
  84. /**********************/
  85.  
  86. int main(int argc, char *argv[]) {
  87.   FILE *fp1,*fp2;
  88.   int len1, len2, strt1=0, strt2=0, pos1, pos2;
  89.   static char b1[buflen], b2[buflen]; /* Amiga !!! */
  90.   int l, diffs=0;
  91.   
  92.   if (argc < 3 || argc >4 || (argc == 4 && (opt_quick=strcasecmp(argv[3],"quick")))) {
  93.     fprintf(stderr,"Cmp 0.1 (%s)\n",__DATE__);
  94.     fprintf(stderr,"usage: %s file file [QUICK]\n",argv[0]);
  95.     exit(20);
  96.   }
  97.   
  98.   if (NULL == (fp1 = fopen(argv[1],"r"))) error1;
  99.   if (NULL == (fp2 = fopen(argv[2],"r"))) error2;
  100.   len1 = getfilesize(fp1);
  101.   len2 = getfilesize(fp2);
  102.   if (fseek(fp1,strt1,SEEK_SET)) error1;
  103.   if (fseek(fp2,strt2,SEEK_SET)) error2;
  104.   pos1 = strt1; pos2 = strt2;
  105.  
  106.   l = buflen;
  107.   while (pos1!=len1 && pos2!=len2) {
  108.     if (len1-pos1 < l) l = len1-pos1;
  109.     if (len2-pos2 < l) l = len2-pos2;
  110.     if (1 != fread(b1, l, 1, fp1)) error1;
  111.     if (1 != fread(b2, l, 1, fp2)) error2;
  112.     diffs += cmp (b1, b2, pos1, pos2, l);
  113.     pos1 += l; pos2 += l;
  114.   }
  115.   
  116.   printf(diffs == 0 ? "files are equal\n" : "files have %d differences\n",diffs);
  117.   if (len1 != len2) printf("file '%s' is %d bytes %s than file '%s'\n",
  118.     argv[1],abs(len1-len2),len1>len2?"larger":"shorter",argv[2]);
  119.   
  120.   if (diffs == 0) return 0; else return 5;
  121. }
  122.  
  123.